package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.UserAddressBookService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
public class UserAddressBookController {
    @Autowired
    private UserAddressBookService userAddressBookService;

    @GetMapping("list")
    @ApiOperation("地址列表查询")
    public Result selectUserList(){
        List<AddressBook> addressBook=userAddressBookService.selectUserList();
        return Result.success(addressBook);
    }

    @PostMapping
    public Result insertAddress(@RequestBody AddressBook addressBook){
        userAddressBookService.insertAddress(addressBook);
        return Result.success();
    }

    @PutMapping("/default")
    public Result addressBookDefault(@RequestBody AddressBook addressBook){
        userAddressBookService.defaultAddressBook(addressBook);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Long id){
        AddressBook addressBook=userAddressBookService.findByid(id);
        return Result.success(addressBook);
    }

    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        userAddressBookService.update(addressBook);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(String id){
        userAddressBookService.delete(id);
        return Result.success();
    }

    @GetMapping("/default")
    public Result selectDefault(){
        AddressBook addressBook=userAddressBookService.selectDefault();
        return Result.success(addressBook);
    }
}
